package com.example.uploadlocalimage;

import java.io.FileNotFoundException;
import java.util.Locale;

import android.app.Activity;
import android.content.ContentResolver;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore.Images;
import android.util.Log;
import android.widget.Toast;

/**
 * 上传单张化验单的控制类类
 * 
 * @author: 李国柱 <guozhu@231team.com>
 */
public class UpLoadImagesActivity extends Activity {

	private static final String TAG = "UpLoadImagesActivity";

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		Intent innerIntent = new Intent(Intent.ACTION_GET_CONTENT); // "android.intent.action.GET_CONTENT"
		innerIntent.setType("image/*"); // 查看类型
		Intent wrapperIntent = Intent.createChooser(innerIntent, null);
		startActivityForResult(wrapperIntent, 2);
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		if (requestCode == 2) {
			if (resultCode == Activity.RESULT_OK) {
				Uri uri = data.getData();
				System.out.println("onActivityResult " + uri.toString());
				ContentResolver cr = this.getContentResolver();
				try {
//					Bitmap labSBm = BitmapFactory.decodeStream(cr.openInputStream(uri));
//					System.out.println("onActivityResult" + labSBm.toString());
					converUri(uri);
				} catch (Exception e) {
					e.getMessage();
				}
			}
		}
	}

	private void converUri(Uri uri) {
		Log.d(TAG + " uri is", "" + uri);
		String uriStr = uri.toString();
		Log.d(TAG + " uri.toString() is", "" + uriStr);
		String path = uri.getEncodedPath();
		Log.d(TAG + " uri.getEncodedPath() is", "" + path);
		if (!checkIsImage(uriStr)) {
			Toast.makeText(this, "请选择图片文件类型", Toast.LENGTH_SHORT).show();
			return;
		}
		if (path != null) {
			Log.d(TAG, "uri_temp is " + path);
		}
	}
	
	private void converUri2(Uri uri) {
		Log.d(TAG + " uri is", "" + uri);
		String uriStr = uri.toString();
		Log.d(TAG + " uri.toString() is", "" + uriStr);
		String path = uri.getEncodedPath();
		Log.d(TAG + " uri.getEncodedPath() is", "" + path);
		if (!checkIsImage(uriStr)) {
			Toast.makeText(this, "请选择图片文件类型", Toast.LENGTH_SHORT).show();
			return;
		}
		if (path != null) {
			path = Uri.decode(path);
			ContentResolver cr = this.getContentResolver();
			StringBuffer buff = new StringBuffer();
			buff.append("(").append(Images.ImageColumns.DATA).append("=").append("'" + path + "'").append(")");
			Cursor cur = cr.query(Images.Media.EXTERNAL_CONTENT_URI, new String[] { Images.ImageColumns._ID },
					buff.toString(), null, null);
			int index = 0;
			for (cur.moveToFirst(); !cur.isAfterLast(); cur.moveToNext()) {
				index = cur.getColumnIndex(Images.ImageColumns._ID);
				// set _id value
				index = cur.getInt(index);
			}
			if (index == 0) {
				// do nothing
			} else {
				Uri uri_temp = Uri.parse("content://media/external/images/media/" + index);
				Log.d(TAG, "uri_temp is " + uri_temp);
				if (uri_temp != null) {
					uri = uri_temp;
				}
			}
		}
	}

	private boolean checkIsImage(String uriStr) {
		boolean isImg = false;
		int sL = uriStr.length();
		String[] formats = { ".jpg", ".png", ".gif", ".bmp", ".tif" };
		String format = uriStr.substring(sL - 4, sL).toLowerCase(Locale.CHINA);
		if ("file".equals(uriStr.substring(0, 4))) {
			for (String string : formats) {
				if (string.equals(format)) {
					isImg = true;
				}
			}
		}
		return isImg;
	}
}
